﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Prisa.Entities;
using Prisa.BussinesLogic;
using Microsoft.Practices.Unity;

namespace Prisa.UI
{
    public partial class frmDeudores : Form
    {
        public frmDeudores()
        {
            InitializeComponent();
            this.BuildUp();
        }

        [Dependency]
        public ICreditoBL creditoBL { get; set; }

        [Dependency]
        public IPagoBL pagoBL { get; set; }

        [Dependency]
        public IDetalleCreditoBL detalleCredBL { get; set; }


        private void frmDeudores_Load(object sender, EventArgs e)
        {
            cREDITOBindingSource.DataSource = creditoBL.GetAllFromCreditos();
        }

        private void cREDITODataGridView_CurrentCellChanged(object sender, EventArgs e)
        {
            var _credito = cREDITOBindingSource.Current as CREDITO;

            if (_credito != null)
            {
                var _detalles = detalleCredBL.GetFromDetalleCreditoByProductoDescAndCreditoId("", _credito.Id);
                var _pagos = pagoBL.GetFromPagosbyCreditoId(_credito.Id);

                decimal saldoFavor = 0;
                decimal totalCredito = 0;

                if (_detalles != null)
                {
                    totalCredito = _detalles.Where(d => d.Pagado == false).Sum(dv => dv.Monto);
                    var newTotalCredito = Math.Round(totalCredito, 2);
                    lblTotalCredito.Text = newTotalCredito.ToString();

                    saldoFavor = _pagos.Sum(dv => dv.Monto) - (_detalles.Where(dv => dv.Pagado == true).Sum(d => d.Monto));

                    var lDeuda = totalCredito - saldoFavor;
                    if (lDeuda > 0)
                    {
                        lblDeuda.Text = lDeuda.ToString();
                    }
                    else lblDeuda.Text = "0.00";
                }

                if (_pagos.Count > 0 && _detalles.Count > 0)
                {
                    saldoFavor = _pagos.Sum(dv => dv.Monto) - (_detalles.Where(dv => dv.Pagado == true).Sum(d => d.Monto));
                    lblSaldoFavor.Text = saldoFavor.ToString();

                    var lDeuda = totalCredito - saldoFavor;
                    if (lDeuda > 0)
                    {
                        lblDeuda.Text = lDeuda.ToString();
                    }
                    else lblDeuda.Text = "0.00";
                }
                else
                {
                    lblSaldoFavor.Text = "0.00";
                    lblDeuda.Text = totalCredito.ToString();
                }
            }
            else
            {
                lblTotalCredito.Text = "0.00";
                lblSaldoFavor.Text = "0.00";
                lblDeuda.Text = "0.00";
            }
        }

       


    }
}
